// Dynamic Dictionary
// Created by Ryan McDougall and Chris Rog
// contact: rp.mcdougall@gmail.com / rog@cericlabs.com
// Version Created: 9/28/2011
// Last Updated: 10/16/2011
// Hosted: code.google.com/p/enhanced-dictionary
// Version 1.2
// Check out README for help
// This program builds wordlists based on input files and uses regular expressions to achieve desired output.

import java.io.*;
import java.util.*;
import java.util.regex.*;

public class exe {

	public static void main(String[] args) throws IOException {
		if(args.length < 2) {
			System.out.println("Invalid arguments. Usage: java exe (regex1).* (regex2).* ");
			System.out.println("View the README for more detailed information on usage.");
			return;
		}

		//Newline Operator
		String newLine = String.format("%n");

		//Random number generator for HuskyNet Example.
		Random intgen = new Random();

		// Linked lists for storing strings from input wordlists.
		List<String> words1 = new LinkedList<String>();
		List<String> words2 = new LinkedList<String>();

		// BufferedReaders/Writers
		BufferedReader freader1 = new BufferedReader(new FileReader("fname.txt"));
		BufferedReader freader2 = new BufferedReader(new FileReader("lname.txt"));
		BufferedWriter fwriter = new BufferedWriter(new FileWriter("output.txt"));

		String s1, s2;

		// Compile the regular expressions. The input expressions require at least one capture
		// group for the program to complete.
		Pattern p1 = Pattern.compile(args[0]);
		Pattern p2 = Pattern.compile(args[1]);


		while((s1 = freader1.readLine()) != null) {
			// Get a matcher for the first regex and compare it to our input
			Matcher m = p1.matcher(s1);

			// Check if the line matches the expression
			if(m.matches()) {
				// If it does, add the part of the line that matched
				words1.add(m.group(1));
			} else {
				// Otherwise, fail.
				System.out.printf("No match: %s\n", s1);
			}
		}

		while((s2 = freader2.readLine()) != null) {
			Matcher m = p2.matcher(s2);
			if(m.matches()) {
				words2.add(m.group(1));
			} else {
				System.out.printf("No match: %s\n", s2);
			}
		}

		// Loop combinations basic for Adam Johnson 'ajohnson' style. Regex1:(.{1}+).* Regex2:(.*).*
    	 for(int i = 0; i < words1.size(); ++i)
				for(int j = 0; j < words2.size(); ++j){
					fwriter.write(words1.get(i) +  words2.get(j) + newLine);
					}

		// Loop combinations with HuskyNet Example Regex1: (.{2}+).* Regex2: (.{2}+).*
		// for(int i = 0; i < words1.size(); ++i)
		//		for(int j = 0; j < words2.size(); ++j){
		//			int randomInt1 = intgen.nextInt(10);
		//			int randomInt2 = intgen.nextInt(10);
		//	   	    fwriter.write(words1.get(i) + words2.get(j) + "0" + randomInt1 + "0" + randomInt2 + newLine);

//			}

		// Loop combinations for example Adam Johnson 'johnsonad' Regex1:(.{2}+).* Regex2:(.*).*
    	// for(int i = 0; i < words1.size(); ++i)
		//	for(int j = 0; j < words2.size(); ++j){
		//		fwriter.write(words2.get(i) +  words1.get(j) + newLine);

		//	}

		// Loop combinations for example Adam Johnson 'johnsonad' + Domain included Regex1:(.{2}+).* Regex2:(.*).*
    	// for(int i = 0; i < words1.size(); ++i)
		//	for(int j = 0; j < words2.size(); ++j){
		//		fwriter.write(words2.get(i) +  words1.get(j) + "@targetdomain.com" + newLine);

		//	}

		// Loop combinations for example Adam Johnson 'adam.johnson' Regex1:(.*).* Regex2:(.*).*
    	//for(int i = 0; i < words1.size(); ++i)
		//	for(int j = 0; j < words2.size(); ++j){
		//		fwriter.write(words1.get(i) + "." +  words2.get(j) + newLine);

		//	}



		fwriter.close();
		System.out.println("Dictionary complete. Open the output.txt file to see results.");
	}


}